﻿package mortal.game.view.daily
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import com.gengine.global.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.guide.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.daily.item.*;
    import mortal.game.view.daily.render.*;
    import mortal.game.view.daily.view.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class NewDailyModule extends BaseWindow
    {
        private var _bg1:ScaleBitmap;
        private var _bg2:ScaleBitmap;
        private var _titleBg:ScaleBitmap;
        private var _titleTxt:GTextFiled;
        private var _tabBar:GTabarNew;
        private var _list:GTileList;
        protected var _redBtn:GButton;
        private var _titleSp:NewDailyTitleSp;
        private var _rect3d:Rect3DObject;
        public static var _isByCustom:Boolean = false;

        public function NewDailyModule(param1:ILayer = null)
        {
            super(param1);
            this.setSize(830, 520);
            title = "" + Language.getString(71043);
            titleHeight = 60;
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        private function vitalitysort(param1:TDailyViewConfig, param2:TDailyViewConfig) : int
        {
            if (DailyUtil.isComplete(param1) && !DailyUtil.isComplete(param2))
            {
                return 1;
            }
            if (!DailyUtil.isComplete(param1) && DailyUtil.isComplete(param2))
            {
                return -1;
            }
            if (param1.id < param2.id)
            {
                return -1;
            }
            return 1;
        }// end function

        private function onTabBarChange(event:MuiEvent = null) : void
        {
            var _loc_4:Boolean = false;
            var _loc_5:int = 0;
            var _loc_6:TDailyViewConfig = null;
            if (this._tabBar == null || this._tabBar.dataProvider == null)
            {
                return;
            }
            if (this._tabBar.selectedIndex >= this._tabBar.dataProvider.length)
            {
                this._tabBar.selectedIndex = this._tabBar.dataProvider.length - 1;
            }
            if (this._tabBar.dataProvider[this._tabBar.selectedIndex] == null)
            {
                return;
            }
            var _loc_2:* = int(this._tabBar.dataProvider[this._tabBar.selectedIndex].name);
            var _loc_3:* = DailyUtil.getDataByGroup(_loc_2);
            DailyUtil.curArr = _loc_3;
            if (_loc_2 == EDailyType._EDailyTypeVitality)
            {
                var _loc_7:Boolean = false;
                this._titleBg.visible = false;
                this._titleSp.visible = _loc_7;
                this._titleTxt.text = "";
                this._list.height = 423;
                this._list.setStyle("cellRenderer", DailyVitalityCellRenderer);
                this._list.columnWidth = 397;
                this._list.rowHeight = 35;
                _loc_3.sort(this.vitalitysort);
                UIFactory.setObjAttri(this._bg1, 8, 62, 417, 452);
                UIFactory.setObjAttri(this._bg2, 427, 62, 400, 452);
            }
            else
            {
                UIFactory.setObjAttri(this._bg1, 8, 62, 571, 452);
                UIFactory.setObjAttri(this._bg2, 581, 62, 245, 452);
                _loc_3.sortOn("id", Array.NUMERIC);
                if (_isByCustom && Cache.instance.task.needLevelToCompleteTask != null && _loc_2 != EDailyType._EDailyTypeTask)
                {
                    _loc_4 = true;
                    _loc_5 = 0;
                    while (_loc_5 < _loc_3.length)
                    {
                        
                        _loc_6 = _loc_3[_loc_5];
                        if (_loc_6 != null && _loc_6.expStar > 0 && Cache.instance.daily.hasTime(_loc_6))
                        {
                            _loc_4 = false;
                            break;
                        }
                        _loc_5++;
                    }
                    if (_loc_4)
                    {
                        this.openByIndex(2);
                        return;
                    }
                }
                if (_loc_2 == EDailyType._EDailyTypeBoss)
                {
                    this._titleTxt.text = "" + Language.getString(71047);
                }
                else
                {
                    this._titleTxt.text = "" + Language.getString(71048);
                }
                var _loc_7:Boolean = true;
                this._titleBg.visible = true;
                this._titleSp.visible = _loc_7;
                this._list.height = 423;
                this._list.setStyle("cellRenderer", NewDailyCellRenderer2);
                this._list.columnWidth = 550;
                this._list.rowHeight = 56;
            }
            if (_loc_2 == 2)
            {
                _loc_3.sort(DailyUtil.sortActive);
            }
            this._list.dataProvider = new DataProvider(_loc_3);
            this._list.selectedIndex = 0;
            this._list.drawNow();
            this.onListChange();
            if (this._tabBar.dataProvider && (this._tabBar.dataProvider.length - 1) == this._tabBar.selectedIndex)
            {
                EffectManager.glowFilterUnReg(this._redBtn);
            }
            return;
        }// end function

        private function onListChange(event:Event = null) : void
        {
            var _loc_4:NewDailyShowBaseView = null;
            if (this._list.dataProvider.length == 0)
            {
                this.cleanAllShowView();
                return;
            }
            var _loc_2:* = this._list.selectedItem as TDailyViewConfig;
            var _loc_3:* = int(this._tabBar.dataProvider[this._tabBar.selectedIndex].name);
            if (_loc_3 == EDailyType._EDailyTypeVitality)
            {
                if (!DailyVitalityShowView.instance.parent)
                {
                    this.cleanAllShowView();
                }
                UIFactory.setObjAttri(DailyVitalityShowView.instance, 430, 65, -1, -1, contentTopOf3DSprite);
                DailyVitalityShowView.instance.num = this._list.dataProvider.length;
                return;
            }
            else if (_loc_3 == EDailyType._EDailyTypeBoss)
            {
                if (!NewDailyBossShowView.instance.parent)
                {
                    this.cleanAllShowView();
                }
                _loc_4 = NewDailyBossShowView.instance;
                NewDailyBossShowView.instance.window = this;
            }
            else if (_loc_2.group == EDailyType._EDailyTypeCopy || _loc_2.group == EDailyType._EDailyTypeTask || _loc_2.group == EDailyType._EDailyTypeActivity)
            {
                if (!DailyCommonShowView.instance.parent)
                {
                    this.cleanAllShowView();
                }
                _loc_4 = DailyCommonShowView.instance;
            }
            UIFactory.setObjAttri(_loc_4, 584, 65, -1, -1, contentTopOf3DSprite);
            _loc_4.data(_loc_2, _loc_2.group);
            return;
        }// end function

        private function cleanAllShowView() : void
        {
            return;
        }// end function

        public function openByIndex(param1:int) : void
        {
            var _loc_2:* = this._tabBar.dataProvider.length;
            param1 = param1 > _loc_2 ? ((_loc_2 - 1)) : (param1);
            this._tabBar.selectedIndex = param1;
            this.onTabBarChange();
            _isByCustom = false;
            return;
        }// end function

        public function openByReward() : void
        {
            if (DailyUtil.hasActiveReward())
            {
                this.openByIndex(3);
            }
            else if (this._tabBar.selectedIndex == -1)
            {
                this.openByIndex(0);
            }
            else
            {
                this.openByIndex(this._tabBar.selectedIndex);
            }
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            param1 = (Global.stage.stageWidth - this.width) / 2;
            param2 = (Global.stage.stageHeight - this.height) / 2;
            super.show(param1, param2);
            this.onAutoGuideRedPointRefresh();
            AutoGuideRedPoint.instance.setValueByName(AutoGuideRedPoint.DailyVitalityAdd, false);
            return;
        }// end function

        private function onAutoGuideRedPointRefresh(event:DataEvent = null) : void
        {
            var _loc_2:Button = null;
            if (AutoGuideRedPoint.instance.getValueByName(AutoGuideRedPoint.DailyVitalityAdd))
            {
                _loc_2 = this._tabBar.tab.getButtonAt(3);
                if (this._tabBar.selectedIndex != 3)
                {
                    EffectManager.glowFilterReg(this._redBtn, [FilterConst.chatTipsFilter], 0.6, 20, 2, 0);
                }
            }
            else if (this._tabBar && this._tabBar.tab.getButtonAt(3))
            {
                _loc_2 = this._tabBar.tab.getButtonAt(3);
                EffectManager.glowFilterUnReg(this._redBtn);
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(581, 63, 0, 0), this);
            this._bg1 = UIFactory.bg(8, 62, 417, 452, this);
            this._bg2 = UIFactory.bg(427, 62, 400, 452, this);
            this._titleBg = UIFactory.bg(4, 62, 575, 26, this, ImagesConst.RegionTitleBg);
            this._titleTxt = UIFactory.gTextField("", 10, 65, 600, 20);
            this._titleSp = UIFactory.getUICompoment(NewDailyTitleSp, 18, 65, this);
            this._redBtn = UIFactory.gButton("" + Language.getString(71049), 253, 32, 79, 27, this);
            this._redBtn.mouseEnabled = false;
            this._redBtn.label = "";
            var _loc_1:* = this.getTabArr();
            this._tabBar = UIFactory.gTabBarNew(0, 32, _loc_1, 830, 520, 80, 26, this, this.onTabBarChange, "TabButtonNew");
            this._tabBar.selectedIndex = -1;
            this._list = UIFactory.tileList(13, 89, 565, 445, this);
            this._list.setStyle("cellRenderer", NewDailyCellRenderer);
            this._list.horizontalGap = 0;
            this._list.verticalGap = 0;
            this._list.direction = GBoxDirection.VERTICAL;
            this._list.configEventListener(Event.CHANGE, this.onListChange);
            Dispatcher.addEventListener(EventName.AutoGuideRedPointRefresh, this.onAutoGuideRedPointRefresh);
            var _loc_2:* = this._tabBar.tab.getButtonAt((_loc_1.length - 1));
            if (_loc_2)
            {
                this._redBtn.x = this._tabBar.x + _loc_2.x + 7;
            }
            else
            {
                this._redBtn.x = this._tabBar.x + (_loc_1.length - 1) * 83 + 8;
            }
            return;
        }// end function

        private function getTabArr() : Array
        {
            var _loc_1:Array = [];
            if (LevelCheckUtil.isLevelEnougth(64))
            {
                _loc_1.push({name:2, label:"" + Language.getString(71050)});
            }
            _loc_1.push({name:1, label:"" + Language.getString(71051)});
            if (LevelCheckUtil.isLevelEnougth(65))
            {
                _loc_1.push({name:4, label:"" + Language.getString(71052)});
            }
            _loc_1.push({name:8, label:"" + Language.getString(71402)});
            return _loc_1;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            _isByCustom = false;
            Dispatcher.removeEventListener(EventName.AutoGuideRedPointRefresh, this.onAutoGuideRedPointRefresh);
            AutoGuideRedPoint.instance.setValueByName(AutoGuideRedPoint.DailyVitalityAdd, false);
            Rect3DManager.instance.disposeRect3d(this._rect3d);
            this._bg1.dispose(param1);
            this._bg2.dispose(param1);
            this._titleBg.dispose(param1);
            this._titleTxt.dispose(param1);
            this._tabBar.dispose(param1);
            this._list.dispose(param1);
            this._bg1 = null;
            this._bg2 = null;
            this._titleBg = null;
            this._titleTxt = null;
            this._tabBar = null;
            this._list = null;
            this._titleSp = null;
            this._redBtn = null;
            this.cleanAllShowView();
            return;
        }// end function

    }
}
